<!DOCTYPE html>
<html>

<head>
    <title>Online Store Seller Page</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
    <header>
        <h2>Online Store Seller page</h2>
    </header>
    <main>
        <div class="content">
            <h3>
                <?php
                session_start();
                $usr = $_SESSION['user'];
                echo "Your seller id is $usr";
                ?>
                <button type="button" class="styledBtn" onclick="window.location.href='LoginPage.html'">Logout</button>
            </h3>
        </div>
        <div class="content">
            <h2 id="top">Order Details</h2>
            <hr>
            <div>
            </div>
            <!--table to display items-->
            <?php
            include "connectStoreDB.php";

            $oid=$_GET["oid"];

            echo "<div><h3>Current order id: $oid</h3><button type='button' style='float:right;' class='styledBtn' onclick=\"window.location.href='seller.php'\">Back</button></div>";

            $sql = "SELECT * FROM order_items JOIN products USING(product_id) WHERE order_id=$oid;";

            $result = mysqli_query($conn, $sql);

            //check if result is empty
            if (mysqli_num_rows($result) > 0) {

                //print the heading of table
                echo "<table border='1'>";
                echo "<tr>";

                //print each column name
                while ($property = mysqli_fetch_field($result)) {
                    echo "<th>" . $property->name . "</th>";
                }
                echo "</tr>";

                //get number of columns
                $fields = mysqli_num_fields($result);

                $total_price=0;
                //Get each row data
                while ($row = mysqli_fetch_array($result)) {
                    echo "<tr>";
                    $total_price+=$row[4]*$row[2];
                    for ($f = 0; $f < $fields; $f++) {
                        echo "<td>" . $row[$f] . "</td>";
                    }
                }
                echo "</table>";
                echo "<h3>Total price is $$total_price</h3>";
            } else {
                echo "0 results";
            }

            //close connection
            mysqli_close($conn);
            ?>
            <br>
            <h3><?php
                include "connectStoreDB.php";

                $oid=$_GET["oid"];
    
                $sql = "SELECT payment_type FROM payments JOIN payment_trans USING (payment_type_id) WHERE order_id=$oid;";
    
                $result = mysqli_query($conn, $sql);

                echo "Payment method: ".mysqli_fetch_array($result)[0];

                //close connection
                mysqli_close($conn);
                ?></h3>
        </div>
        <div class="content narrowbox">
            <h2>Order reviews</h2>
            <hr>
            <?php
            include "connectStoreDB.php";

            $oid=$_GET["oid"];

            $sql = "SELECT * FROM order_reviews WHERE order_id=$oid;";

            $result = mysqli_query($conn, $sql);

            //check if result is empty
            if (mysqli_num_rows($result) > 0) {
                $index=0;
                while ($row = mysqli_fetch_assoc($result)) {
                    $comment=$row["review_comment_message"];
                    $date=$row["review_comment_date"];

                    //ignore empty columns
                    if($comment!=""){
                        $index++;
                        echo "<h4>[$index]: </h4>".$comment."<br><span style='font-style:italic;color:grey'>reviewed on $date</span><br><br>";
                    }
                }
                echo "<hr>".$index." results";
            } else {
                echo "0 results";
            }

            //close connection
            mysqli_close($conn);
            ?>
        </div>
    </main>

</body>

</html>